#!/bin/bash

# 1. 先停止当前运行的 Node.js 服务
echo "Stopping existing Node.js service..."
cd /opt/nest-server # 确保进入正确的项目目录
npm run prod:stop

# 2. 删除当前目录下的 dist 文件夹
echo "Removing existing dist folder..."
rm -rf dist

# 3. 解压 dist.zip 文件到当前目录
echo "Unzipping dist.zip in the current directory..."
unzip dist.zip

# 4. 启动 Node.js 服务
echo "Starting Node.js service with PM2..."
npm run prod:pm2

# 5. 实时打印 PM2 的日志（最后 100 行）
echo "Displaying the last 100 lines of PM2 logs..."
pm2 logs --lines 100

echo "Deployment and service restart completed successfully!"
